<?php

namespace Admin\Controller;
use Think\Controller;

class StudentController extends CommonController {
    public function index(){
        $this->display();
    }

	// 学生预报到
	public function forecast() {
		// 显示学生信息
		$student = M('student');
    	$map = ['studentName'=>session('adminName')];
    	$res = $student->where($map)->select();
        $this->assign('res',$res);
        // 分配宿舍
        if(IS_AJAX){
            $id = I('post.idcard');
            $map2 = ['idCard'=>$id];
            $m = D('student');
            $data = ['forecast'=>'1'];
            $dormitory = D('dormitory');
        	// 查询床位剩余
        	$idle = $dormitory->field('idle')->where('idle','!=','0')->limit(1)->find();
        	// 查询还剩有床位的宿舍号
        	$dor_dormitory = $dormitory->field('dormitory')->where('idle','!=','0')->limit(1)->find();
            
    		// 判断床位剩余
    		if($idle != null) {
    			// 查询第一个床位
    			$one = $dormitory->field('one')->where('idle','!=','0')->limit(1)->find()['one'];
    			// 查询第二个床位
    			$two = $dormitory->field('two')->where('idle','!=','0')->limit(1)->find()['two'];
				// 查询第三个床位
    			$three = $dormitory->field('three')->where('idle','!=','0')->limit(1)->find()['three'];
    			// 查询第四个床位
    			$four = $dormitory->field('four')->where('idle','!=','0')->limit(1)->find()['four'];
    			// 查询第五个床位
    			$five = $dormitory->field('five')->where('idle','!=','0')->limit(1)->find()['five'];
    			// 查询第六个床位
    			$six = $dormitory->field('six')->where('idle','!=','0')->limit(1)->find()['six'];
    			
    			// 判断第一个床位有没有人
        		if(empty($one)) {
        			// 分配宿舍1号床位
        			$data_one = ['one'=>session('adminName')];
            		$dormitory->field('one')->where('idle','!=','0')->limit(1)->save($data_one);
            		// 床位数减一
            		$dormitory->field('idle')->where('idle','!=','0')->limit(1)->setDec('idle',1);
            		// 修改学生的床位
            		$student->where($map2)->save($dor_dormitory);
            		// 预报到
        			$res2 = $m->where($map2)->save($data);
            		if($res2) {
            			$out = [
		                    'status'=>1,
		                    'info'=>'成功预报到'
		                ];
            		}
            		$this->ajaxReturn($out);
            		exit;
		        }else if(empty($two)) {
		          	// 分配宿舍2号床位
        			$data_two = ['two'=>session('adminName')];
            		$dormitory->field('two')->where('idle','!=','0')->limit(1)->save($data_two);
        			// 床位数减一
            		$dormitory->field('idle')->where('idle','!=','0')->limit(1)->setDec('idle',1);
            		// 修改学生的床位
            		$student->where($map2)->save($dor_dormitory);
            		// 预报到
        			$res2 = $m->where($map2)->save($data);
            		if($res2) {
            			$out = [
		                    'status'=>1,
		                    'info'=>'成功预报到'
		                ];
            		}
            		$this->ajaxReturn($out);
            		exit;
		        }else if(empty($three)) {
		          	// 分配宿舍3号床位
        			$data_three = ['three'=>session('adminName')];
            		$dormitory->field('three')->where('idle','!=','0')->limit(1)->save($data_three);
        			// 床位数减一
            		$dormitory->field('idle')->where('idle','!=','0')->limit(1)->setDec('idle',1);
            		// 修改学生的床位
            		$student->where($map2)->save($dor_dormitory);
            		// 预报到
        			$res2 = $m->where($map2)->save($data);
            		if($res2) {
            			$out = [
		                    'status'=>1,
		                    'info'=>'成功预报到'
		                ];
            		}
            		$this->ajaxReturn($out);
            		exit;
		        }else if(empty($four)) {
		          	// 分配宿舍4号床位
        			$data_four = ['four'=>session('adminName')];
            		$dormitory->field('four,idle')->where('idle','!=','0')->limit(1)->save($data_four);
        			// 床位数减一
            		$dormitory->field('idle')->where('idle','!=','0')->limit(1)->setDec('idle',1);
            		// 修改学生的床位
            		$student->where($map2)->save($dor_dormitory);
            		// 预报到
        			$res2 = $m->where($map2)->save($data);
            		if($res2) {
            			$out = [
		                    'status'=>1,
		                    'info'=>'成功预报到'
		                ];
            		}
            		$this->ajaxReturn($out);
            		exit;
		        }else if(empty($five)) {
		          	// 分配宿舍5号床位
        			$data_five = ['five'=>session('adminName')];
            		$dormitory->field('five,idle')->where('idle','!=','0')->limit(1)->save($data_five);
        			// 床位数减一
            		$dormitory->field('idle')->where('idle','!=','0')->limit(1)->setDec('idle',1);
            		// 修改学生的床位
            		$student->where($map2)->save($dor_dormitory);
            		// 预报到
        			$res2 = $m->where($map2)->save($data);
            		if($res2) {
            			$out = [
		                    'status'=>1,
		                    'info'=>'成功预报到'
		                ];
            		}
            		$this->ajaxReturn($out);
            		exit;
		        }else if(empty($six)) {
		          	// 分配宿舍6号床位
        			$data_six = ['six'=>session('adminName')];
            		$dormitory->field('six,idle')->where('idle','!=','0')->limit(1)->save($data_six);
        			// 床位数减一
            		$dormitory->field('idle')->where('idle','!=','0')->limit(1)->setDec('idle',1);
            		// 修改学生的床位
            		$student->where($map2)->save($dor_dormitory);
            		// 预报到
        			$res2 = $m->where($map2)->save($data);
            		if($distribution) {
            			$out = [
		                    'status'=>1,
		                    'info'=>'成功预报到'
		                ];
            		}
            		$this->ajaxReturn($out);
            		exit;
		        }
	        }else {
          		$out = [
	                    'status'=>0,
	                    'info'=>'宿舍满了!预报到失败！'
              	];
          	}
            $this->ajaxReturn($out);
        }
		$this->display();
	}
	
	// 学生信息完善
	public function report() {
		// 显示学生信息
		$student = M('student');
        $map = ['studentName'=>session('adminName')];
    	$res = $student->where($map)->find();
        $this->assign('res',$res);
        // 信息完善
        if(IS_AJAX){
        	$idcard =I('post.idcard');
        	$studentsex =  I('post.studentsex');
        	$sutdentnumber =I('post.studentnumber');
        	$address =I('post.address');
        	$sick =I('post.sick');
        	$prove =I('post.prove');
        	$insurance =I('post.insurance');
            
            $data = [
                'studentSex' => $studentsex,
                'studentNumber' => $sutdentnumber,
                'address' => $address,
                'sick' => $sick,
                'prove' => $prove,
                'insurance' => $insurance
            ];
            $map  = ['idCard'=>$idcard];
            $m = D('student');
            $res = $m->where($map)->save($data);
            if ($res) {
                $out = [
                    'info' => '保存成功',
                    'status'=>1
                ];
            } else {
                $out = [
                    'info' => '保存失败，请检查信息是否完整或未进行修改',
                    'status'=>0
                ];
            }
            $this->ajaxReturn($out);
        }
		$this->display();
	}
	
	// 个人信息
	public function home() {
		// 显示学生信息
		$student = M('student');
    	$map = ['studentName'=>session('adminName')];
    	$res = $student->where($map)->find();
        $this->assign('res',$res);
		$this->display();
	}
}
?>